package com.gouwudai.gobbs.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IMessageDAO;
import com.gouwudai.gobbs.pojo.TbbsMessage;
import com.litt.core.exception.DBException;

public class MessageDAOImpl extends BaseDAOImpl implements IMessageDAO 
{
	private static final Logger logger = Logger.getLogger(MessageDAOImpl.class);	
	private static final String BUSI_NAME = "短消息";
	
	public void save(TbbsMessage tbbsMessage)
	{    		
		super.saveObject(tbbsMessage);		
	}
	
	public TbbsMessage load(Integer id)
	{
		return (TbbsMessage)super.loadObject(TbbsMessage.class, id);
	} 
	
    public void update(TbbsMessage tbbsMessage)
    {
    	StringBuilder hql = new StringBuilder();
        hql.append("update TbbsMessage set takeUser=?"); 
        hql.append(", title=?");
        hql.append(", content=?");      
        hql.append(" where messageId=?");
        super.updateObject(hql.toString(), new Object[]{
        	tbbsMessage.getTakeUser(),
        	tbbsMessage.getTitle(),
        	tbbsMessage.getContent(),
        	tbbsMessage.getMessageId()});
    }
    
    
    public void send(Integer messageId)
    {
    	StringBuilder hql = new StringBuilder();
        hql.append("update TbbsMessage set sendFlag=1");             
        hql.append(" where messageId=?");
        super.updateObject(hql.toString(), new Object[]{messageId});
    }    
    
    
    public void delete(String messageIds)
    {
        super.deleteObjects(TbbsMessage.class.getName(),"messageId",messageIds);
    }
    
    /**
     * 草稿箱
     * @param cond
     * @param pageIndex
     * @param pageSize
     * @param sort
     * @return
     * @throws DBException
     */
    public IPageList listDraft(String loginId,int pageIndex,int pageSize)
    {       
    	String listSql = "SELECT * FROM TbbsMessage WHERE SEND_USER='"+loginId+"' AND SEND_FLAG=2  ORDER BY MESSAGE_ID DESC";
  	
    	return super.listSqlPage(listSql.toString(),pageIndex,pageSize);
    }
    
    /**
     * 发件箱
     * @param cond
     * @param pageIndex
     * @param pageSize
     * @param sort
     * @return
     * @throws DBException
     */
    public IPageList listSend(String loginId,int pageIndex,int pageSize)
    {       
    	String listSql = "SELECT * FROM TbbsMessage WHERE SEND_USER='"+loginId+"' AND SEND_FLAG=1 ORDER BY MESSAGE_ID DESC";
 
    	return super.listSqlPage(listSql.toString(),pageIndex,pageSize);
    }    
    
    /**
     * 收件箱
     * @param cond
     * @param pageIndex
     * @param pageSize
     * @param sort
     * @return
     * @throws DBException
     */
    public IPageList listTake(String loginId,int pageIndex,int pageSize)
    {       
    	String listSql = "SELECT * FROM TbbsMessage WHERE TAKE_USER='"+loginId+"' AND SEND_FLAG<>2 ORDER BY MESSAGE_ID DESC";
   	
    	return super.listSqlPage(listSql.toString(),pageIndex,pageSize);
    } 
    
    /**
     * 查询当前登陆用户未读短信
     * @param loginId
     * @return
     * @throws DBException
     */
    public List listNew(String loginId)
    {
    	String hql = "from TbbsMessage where readFlag=0 and takeUser='"+loginId+"'  ORDER BY messageId DESC";
    	return super.listAll(hql);
    }
    
    
}
